package rhc.singleNodeSpider.spider.component;

import java.util.Iterator;
import java.util.List;

import rhc.common.data.dao.SpiderUrlStorage;
import rhc.common.data.dao.SuperDao;
import rhc.common.entity.MyUrl;
import rhc.common.util.RequestUtil;
import rhc.spider.crawler.Crawler;
import rhc.spider.request.Request;
import rhc.spider.schedule.BasicSchedule;
import rhc.spider.setting.Setting;

/**单节点爬虫调度器
 * @author rhc
 *
 */
public class SingleNodeSpiderSchedule extends BasicSchedule {

	/**
	 * 任务id
	 */
	private String taskId;
	
	private SuperDao superDao;
	
	private SpiderUrlStorage urlStorage;
	
	/**
	 * 每一次从数据库取出的url数量
	 */
	private int oneCatchUrlCount=100;
	
	public SingleNodeSpiderSchedule(Crawler crawler, Setting setting,String taskId) {
		super(crawler,setting);
		this.taskId=taskId;
		
		superDao=SuperDao.superDao;
		
		urlStorage=new SpiderUrlStorage(taskId,superDao);
	}

	@Override
	public void addRequest(Request request) {
		MyUrl myUrl=request.getUrl();
		
		try {
			if(!urlStorage.containsUrl(myUrl)){
				urlStorage.addUrl(myUrl);
				super.addRequest(request);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public boolean hasRequest() {
		boolean memoryHaveRequest=super.hasRequest();
		if(memoryHaveRequest){
			return true;
		}
		fromDbLoadRequest();
		return super.hasRequest();
	}

	@Override
	public Iterator<Request> iterator() {
		return new RequestIterator();
	}

	/**
	 * 从数据库加载url
	 */
	private void fromDbLoadRequest(){
		List<MyUrl> myUrlList=urlStorage.getUrlList(oneCatchUrlCount);
		
		if(null != myUrlList && !myUrlList.isEmpty()){
			return;
		}
		
		for(MyUrl myUrl:myUrlList){
			Request request=RequestUtil.fromMyUrlGetRequest(myUrl);
			super.addRequest(request);
		}
	}
	
	/**获取下一个请求
	 * @return
	 */
	protected Request getNextRequest(){
		Request request=super.getNextRequest();
		if(null == request){
			fromDbLoadRequest();
		}
		request=super.getNextRequest();
		
		return request;
	}
	
	/**迭代器
	 * @author lenovo1
	 *
	 */
	private class RequestIterator implements Iterator<Request>{

		@Override
		public boolean hasNext() {
			return hasRequest();
		}

		@Override
		public Request next() {
			return getNextRequest();
		}

		@Override
		public void remove() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}
